package com.zcc.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

import static springfox.documentation.service.ApiInfo.DEFAULT_CONTACT;


@Configuration
//开启Swagger
@EnableSwagger2
public class SwaggerConfig {

    Contact contact=new Contact("zcc","Localhost:8080/","zcc@zcc.com");

    //配置了Swagger的Docket的Bean实例
    @Bean
    public Docket docket(Environment environment) {
        //定义profiles对象，包括了dev和test环境
        Profiles profiles = Profiles.of("dev", "test");
        //获取项目环境，与profiles进行比对，如果一致，也就是处于dev或者test环境，返回true
        //这样就可以判断是否启用swagger，可以使上线后的项目无法通过浏览器使用swagger
        boolean flag = environment.acceptsProfiles(profiles);
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //开关swagger在浏览器中能否访问，false为关闭
                .enable(flag)
                //设置组名，用于文档切换
                //其他人的Docket实例，会有其他分组
                .groupName("zcc")
                //配置扫描接口
                .select()
                //用包扫描，指定目录，basePackage
                //any()：扫描全部
                //none()：不扫描
                //withClassAnnotation()：扫描类上的注解，GetMapping这种
                //withMethodAnnotation()：扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.zcc.controller"))
                //过滤
                //ant()：按照路径过滤
                //.paths(PathSelectors.ant("/api/**"))
                .build();
    }

    public ApiInfo apiInfo() {
        return new ApiInfo(
                "郑冲冲的Swagger文档",
                "测试Swagger",
                "1.0",
                "urn:tos",
                contact,
                "Apache 2.0",
                "http://www.apache.prg/licensses/LICENSE-2.0",
                new ArrayList());
    }
}
